//
//  ThemeManager.m
//  CloudWeibo
//
//  Created by CLOUD on 11/20/14.
//  Copyright (c) 2014 Cloud. All rights reserved.
//

#import "ThemeManager.h"

static ThemeManager *sigleton = nil;

@implementation ThemeManager

+ (ThemeManager *)shareInstance {
    @synchronized(self) {
        if (sigleton == nil) {
            sigleton = [[ThemeManager alloc] init];
        }
        return sigleton;
    }
}

- (id)init {
    self = [super init];
    if (self) {
        NSString *themePath = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"];
        self.themesPlist = [NSDictionary dictionaryWithContentsOfFile:themePath];
        self.themeName = nil;
    }
    return self;
}

- (NSString *)getThemePath {
    if ([self.themeName isEqualToString:@"default"]) {
        NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
        return resourcePath;
    }
    
    NSString *themePath = [self.themesPlist objectForKey:_themeName];
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString *path = [resourcePath stringByAppendingPathComponent:themePath];
    return path;
}

- (UIImage *)getThemeImage:(NSString *) imageName{
    if (imageName.length == 0) {
        return nil;
    }
    
    NSString *themePath = [self getThemePath];
    NSString *imagePath = [themePath stringByAppendingPathComponent:imageName];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    return image;
}

- (UIColor *)getThemeColor:(NSString *) colorName{
    if (colorName.length == 0) {
        return nil;
    }
    NSString *themePath = [self getThemePath];
    NSString *colorPath = [themePath stringByAppendingPathComponent:@"fontColor.plist"];
    self.fontColorPlist = [NSDictionary dictionaryWithContentsOfFile:colorPath];
    return [self getColorWithName:colorName];
    
}

- (UIColor *)getColorWithName:(NSString *)name {
    if (name.length == 0) {
        return nil;
    }
    
    //返回三色值，如：24,35,60
    NSString *rgb = [_fontColorPlist objectForKey:name];
    NSArray *rgbs = [rgb componentsSeparatedByString:@","];
    if (rgbs.count == 3) {
        float r = [rgbs[0] floatValue];
        float g = [rgbs[1] floatValue];
        float b = [rgbs[2] floatValue];
        UIColor *fontColor = Color(r, g, b, 1);
        return fontColor;
    }
    
    return nil;
}


//限制当前对象创建多实例
#pragma mark - sengleton setting
+ (id)allocWithZone:(NSZone *)zone {
    @synchronized(self) {
        if (sigleton == nil) {
            sigleton = [super allocWithZone:zone];
        }
    }
    return sigleton;
}

+ (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)retain {
    return self;
}

- (unsigned)retainCount {
    return UINT_MAX;
}

- (oneway void)release {
}

- (id)autorelease {
    return self;
}

@end
